#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
Simple wx app using menu
"""

import wx


class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title, size=(500, 500))
        self.initUI()

    def initUI(self):
        menu_bar = wx.MenuBar()
        filemenu = wx.Menu()
        view_menu = wx.Menu()

        #Create file menu
        fmenu_new = filemenu.Append(wx.ID_NEW, '&New')
        fmenu_open = filemenu.Append(wx.ID_OPEN, '&Open')
        fmenu_save = filemenu.Append(wx.ID_SAVE, '&Save')
        filemenu.AppendSeparator()

        #Import sub menu
        imp_sub_menu = wx.Menu()
        imp_menu_import_newsfeed = imp_sub_menu.Append(wx.ID_ANY, 'Import news feed list')
        imp_sub_menu_import_bookmarks = imp_sub_menu.Append(wx.ID_ANY, 'Import bookmarks')
        imp_sub_menu_import_mail = imp_sub_menu.Append(wx.ID_ANY, 'Import mail')
        filemenu.AppendMenu(wx.ID_ANY, '&Import', imp_sub_menu)
        filemenu.AppendSeparator()
        fmenu_quit = filemenu.Append(wx.ID_EXIT, 'Quit', 'Quit application')

        #Create view menu
        self.shst = view_menu.Append(wx.ID_ANY, 'Show statusbar',
                                     'Show Statusbar', kind=wx.ITEM_CHECK)
        self.shtl = view_menu.Append(wx.ID_ANY, 'Show toolbar',
                                     'Show Toolbar', kind=wx.ITEM_CHECK)

        view_menu.Check(self.shst.GetId(), True)
        view_menu.Check(self.shtl.GetId(), True)

        self.Bind(wx.EVT_MENU, self.ToggleStatusBar, self.shst)
        self.Bind(wx.EVT_MENU, self.ToggleToolBar, self.shtl)

        self.toolbar = self.CreateToolBar()
        #We could not resize the icon image programmatically using code below.
        #icon.SetWidth(50)
        #icon.SetHeight(50)
        icon = wx.Bitmap('texit_small.png')
        qtool = self.toolbar.AddLabelTool(wx.ID_ANY, 'Quit', icon)
        self.toolbar.Realize()
        self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)

        self.statusbar = self.CreateStatusBar()
        self.statusbar.SetStatusText('Ready')

        self.SetSize((350, 250))
        self.SetTitle('Check menu item')

        menu_bar.Append(filemenu, '&File')
        menu_bar.Append(view_menu, '&View')
        self.SetMenuBar(menu_bar)
        self.Bind(wx.EVT_MENU, self.OnNew, fmenu_new)
        self.Bind(wx.EVT_MENU, self.OnQuit, fmenu_quit)
        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
        self.Move((200, 200))
        self.Center()
        self.Show(True)

    def OnRightDown(self, e):
        self.PopupMenu(ContextMenu(self), e.GetPosition())

    def ToggleStatusBar(self, e):
        if self.shst.IsChecked():
            self.statusbar.Show()
        else:
            self.statusbar.Hide()

    def ToggleToolBar(self, e):
        if self.shtl.IsChecked():
            self.toolbar.Show()
        else:
            self.toolbar.Hide()

    def OnQuit(self, e):
        print e
        print dir(e)
        self.Close()

    def OnNew(self, e):
        print e
        

class ContextMenu(wx.Menu):
    def __init__(self, parent):
        super(ContextMenu, self).__init__()
        self.parent = parent
        
        mmi = wx.MenuItem(self, wx.NewId(), 'Minimize')
        self.AppendItem(mmi)
        self.Bind(wx.EVT_MENU, self.OnMinimize, mmi)
        
        cmi = wx.MenuItem(self, wx.NewId(), 'Close')
        self.AppendItem(cmi)
        self.Bind(wx.EVT_MENU, self.OnClose, cmi)
        
    def OnMinimize(self, e):
        self.parent.Iconize()
        
    def OnClose(self, e):
        self.parent.Close()

if __name__ == '__main__':
    app = wx.App()
    Example(None, "Fun Sizable Window")
    app.MainLoop()

